package com.plian.system.service.fr.audit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.plian.Tools.CollectionUtil;
import com.plian.Tools.DateUtil;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.system.dto.fr.audit.AuditReportDTO;
import com.plian.system.entity.fr.audit.AuditContact;
import com.plian.system.mapper.fr.audit.AuditContactMapper;
import com.plian.system.mybatis.base.BaseServiceImpl;
import com.plian.system.service.fr.audit.IAuditContactService;
import com.plian.system.vo.fr.audit.AuditContactStatisticsVO;
import com.plian.system.vo.report.HeaderVO;
import com.plian.system.vo.report.ReportReturnVO;
import com.plian.system.vo.report.ReportVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 审计成果利用情况 服务实现类
 *
 * @author gangan
 * @since 2019-09-27
 */
@Slf4j
@Service
@AllArgsConstructor
public class AuditContactServiceImpl extends BaseServiceImpl<AuditContactMapper, AuditContact> implements IAuditContactService {

    @Override
    public Boolean deleteLogicByReportId(List<Long> ids) {
        LambdaQueryWrapper<AuditContact> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(AuditContact::getAuditReportId, ids);
        List<AuditContact> auditContacts = list(queryWrapper);
        if (!CollectionUtils.isEmpty(auditContacts)){
            return deleteLogic(auditContacts.stream().map(AuditContact::getId).collect(Collectors.toList()));
        }
        return true;
    }

    @Override
    public List<AuditContact> queryByReportId(Long reportId) {
        LambdaQueryWrapper<AuditContact> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AuditContact::getAuditReportId, reportId);
        return list(queryWrapper);
    }

    @Override
    public ReportReturnVO auditContactStatistics(AuditReportDTO auditReportDTO){

        if (StringUtil.isBlank(auditReportDTO.getFillingYear())){
            auditReportDTO.setFillingYear(DateUtil.format(DateUtil.now(), DateUtil.PATTERN_YEAR));
        }
        List<AuditContactStatisticsVO> auditContactStatisticsVOS = baseMapper.auditContactStatisticsList(auditReportDTO);
        if (!CollectionUtils.isEmpty(auditContactStatisticsVOS)){
            for (AuditContactStatisticsVO statisticsVO : auditContactStatisticsVOS){
                String liaison = "";
                List<AuditContact> auditContacts = new ArrayList<>();
                for (AuditContact auditContact : statisticsVO.getAuditContacts()){
                    if ("042003".equals(auditContact.getProjectId())){
                        if (StringUtil.isNotBlank(liaison)){
                            liaison += StringPool.COMMA;
                        }
                        liaison += auditContact.getName() + (StringUtil.isNotBlank(auditContact.getContactNumber()) ? StringPool.COLON + auditContact.getContactNumber():"");
                    }else {
                        auditContacts.add(auditContact);
                    }
                }
                statisticsVO.setAuditContacts(auditContacts);
                statisticsVO.setLiaison(liaison);
            }
        }
        ReportReturnVO reportReturnVO = new ReportReturnVO();
        ReportVO reportVO = new ReportVO();

        reportVO
                .addHeader(HeaderVO.builder().key("companyName").title("单位").build())
                .addHeader(HeaderVO.builder().key("name").title("姓名").build())
                .addHeader(HeaderVO.builder().key("jobNameId").title("职务").build())
                .addHeader(HeaderVO.builder().key("contactNumber").title("联系电话").build())
                .addHeader(HeaderVO.builder().key("liaison").title("联络员").build())
                .addHeader(HeaderVO.builder().key("isReport").title("制度上报（有/否）").build());
        reportVO.setData(auditContactStatisticsVOS);
        reportReturnVO.setGrids(reportVO);
        return reportReturnVO;
    }

    @Override
    public List<AuditContactStatisticsVO> getExportAuditContactStatistics(AuditReportDTO auditReportDTO) {
        if (StringUtil.isBlank(auditReportDTO.getFillingYear())){
            auditReportDTO.setFillingYear(DateUtil.format(DateUtil.now(), DateUtil.PATTERN_YEAR));
        }
        List<AuditContactStatisticsVO> auditContactStatisticsVOS = baseMapper.auditContactStatisticsList(auditReportDTO);
        List<AuditContactStatisticsVO> results = new ArrayList<>();
        if (!CollectionUtils.isEmpty(auditContactStatisticsVOS)){
            int count = 1;
            for (AuditContactStatisticsVO statisticsVO : auditContactStatisticsVOS){
                results.add(statisticsVO);
                String liaison = "";
                List<AuditContact> old = statisticsVO.getAuditContacts();
                if (CollectionUtil.isNotEmpty(old)){
                    for (int i = 0; i < old.size(); i++){
                        AuditContact auditContact = old.get(i);
                        if ("042003".equals(auditContact.getProjectId())){
                            if (StringUtil.isNotBlank(liaison)){
                                liaison += StringPool.COMMA;
                            }
                            liaison += auditContact.getName() + (StringUtil.isNotBlank(auditContact.getContactNumber()) ? StringPool.COLON + auditContact.getContactNumber():"");
                        }else {
                            if (i == 0){
                                statisticsVO.setIndex(count);
                                statisticsVO.setName(auditContact.getName());
                                statisticsVO.setJobNameId(auditContact.getJobNameId());
                                statisticsVO.setContactNumber(auditContact.getContactNumber());
                            }else {
                                AuditContactStatisticsVO child = new AuditContactStatisticsVO();
                                child.setName(auditContact.getName());
                                child.setJobNameId(auditContact.getJobNameId());
                                child.setContactNumber(auditContact.getContactNumber());
                                results.add(child);
                            }
                        }
                    }

                }
                count++;
                statisticsVO.setLiaison(liaison);

            }
        }
        return results;
    }
}
